package cn.flying.cloud.oauth.server.configuration.handler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;

import com.alibaba.fastjson2.JSON;

import cn.flying.cloud.base.common.enums.RtCodeEnum;
import cn.flying.cloud.base.common.vo.Rt;

/**
 * OAuth认证异常处理
 *
 * @author: admin
 * @date: 2024年04月30日 16:48
 * @version: 1.0
 */
public class CustomOAuth2AuthenticationEntryPointHandler extends OAuth2AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        logger.error("客户端认证异常", exception);

        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(Rt.error(RtCodeEnum.R10020)));
        out.flush();
        out.close();
    }
}
